package com.lanou.controller;

import com.lanou.bean.*;
import com.lanou.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RequestMapping("port")
@Controller
public class PortController {

    @Autowired
    private VipInfoService vipInfoService;

    @Autowired
    private AddrInfoService addrInfoService;

    @Autowired
    private ProvinceService provinceService;

    @Autowired
    private CityService cityService;

    @Autowired
    private CpnUseRecordService cpnUseRecordService;

    @Autowired
    private CpnInfoService cpnInfoService;

    //验证登录的接口(接收账户和密码)
    @RequestMapping("/viplogin")
    @ResponseBody
    public VipInfo Viplogin(String vipAccount, String vipPassword) {
        System.out.println(vipAccount + vipPassword);
        //根据account和password进行验证登录
        VipInfo vipInfo = vipInfoService.selectVipInfoByAP(vipAccount, vipPassword);
        System.out.println("登陆成功,返回会员对象-------" + vipInfo);
        return vipInfo;
    }


    //注册验证的接口,如果已经被注册,返回对象,没有注册,(根据vipAccount判断是否被注册)
    @RequestMapping(value = "/registVerify/{vipAccount}", method = RequestMethod.GET)
    @ResponseBody
    public VipInfo Vipregist(@PathVariable("vipAccount") String vipAccount) {
        System.out.println(vipAccount);
        //根据vip_account判断是否已被注册
        VipInfo vipInfo1 = vipInfoService.selectVipInfoByVA(vipAccount);
        if(vipInfo1!=null){
            System.out.println("用户名已存在");
        }else {
            System.out.println("用户名不存在,可以进行注册");
        }
        return vipInfo1;
    }

    //进行注册
    @RequestMapping(value = "/regist",method = RequestMethod.POST)
    @ResponseBody
    public int RegistToVip(VipInfo vipInfo) {

        System.out.println("进行注册,从前端获取的信息-----"+vipInfo);

        String vipNo = vipInfoService.selectMaxVipNo();
        System.out.println("获取到的最大的vipNo-----"+vipNo);
        int a=(Integer.parseInt(vipNo)+1);
        //根据省市向addr_info表插入数据 (vip_no)
        AddrInfo addrInfo=new AddrInfo();
        addrInfo.setVipNo(String.valueOf(a));
        addrInfo.setProvinceId(vipInfo.getProvinceId());
        addrInfo.setCityId(vipInfo.getCityId());
        addrInfo.setIsPrimary("y");
        addrInfo.setIsUsed("y");
        addrInfo.setInstDate(new Date());
        addrInfo.setInstId(1);
        addrInfo.setMdfDate(new Date());
        addrInfo.setMdfId(1);
        addrInfo.setAddrInfo(vipInfo.getAddrInfo());
        int addrResult = vipInfoService.insertSelective(addrInfo);
//        System.out.println("地址信息的插入------"+addrResult);

        //向vipInfo表插入数据
        //根据vip_no查询addrInfo的id,将addr_id存入addr_info
        int addr_id = vipInfoService.selectAddrIdByVipNo(String.valueOf(a));
        System.out.println(a);
        vipInfo.setVipNo(String.valueOf(a));
        vipInfo.setAddrId(addr_id);
        vipInfo.setRegDate(new Date());
        vipInfo.setVipLevel(0);
        vipInfo.setAccPoint(0);
        vipInfo.setAccSum(0.0);
        vipInfo.setSumMonney(0.0);
        int vipResult = vipInfoService.insertSelective(vipInfo);
//        System.out.println(vipResult);

        //向bankInfo插入银行卡信息
        BankInfo bankInfo=new BankInfo();
        bankInfo.setVipNo(String.valueOf(a));
        bankInfo.setBankCardNo(vipInfo.getBankCardNo());
        bankInfo.setBankName(vipInfo.getBankName());
        bankInfo.setIsUsed("y");
        bankInfo.setInstDate(new Date());
        bankInfo.setInstId(1);
        bankInfo.setMdfDate(new Date());
        bankInfo.setBankType(vipInfo.getBankType());
        int bankResult = vipInfoService.insertSelective(bankInfo);
//        System.out.println("向银行插入数据结果-------"+bankResult);
        if(addrResult>0&&vipResult>0&&bankResult>0){
            return 1;
        }
        return 0;
    }

    //根据vipId增加积分和账户余额
    @RequestMapping("/updateVipInfoByPointAndsum")
    @ResponseBody
    public String updateVipInfoByPointAndsum(Integer id, Integer accPoint, Double accSum) {
        System.out.println("接收到的用户id accpoint accsum" + id + "--" + accPoint + "--" + accSum);
        VipInfo vipInfo = new VipInfo();
        vipInfo.setId(id);
        vipInfo.setAccPoint(accPoint);
        vipInfo.setAccSum(accSum);
        int result = vipInfoService.updateByIdSelective(vipInfo);
        System.out.println("返回结果----" + result);
        if (result > 0) {
            return "success";
        } else {
            return "defeat";
        }
    }


    //根据身份证号查询会员对象
    @RequestMapping(value = "/vipInfo/{idCard}", method = RequestMethod.GET)
    @ResponseBody
    public VipInfo selectVipInfoByIdCard(@PathVariable("idCard") String idCard) {
        VipInfo vipInfo = vipInfoService.selectVipInfoByIdCard(idCard);
        return vipInfo;
    }


    //前端页面我的惠买个人信息更新,(根据id查询会员信息)
    @RequestMapping(value = "getvipInfoById.do")
    @ResponseBody
    public VipInfo getVipInfo(Integer id){
        //接收id,根据id查询会元信息
        System.out.println("接收到的id------"+id);
        VipInfo  vipInfo = vipInfoService.selectByPrimaryKey(id);

        //根据addrid作为addrInfo的id查询详细地址信息
        Integer addrId = vipInfo.getAddrId();
        AddrInfo addrInfo = addrInfoService.selectByPrimaryKey(addrId);
        vipInfo.setAddrInfo(addrInfo.getAddrInfo());
        System.out.println("获取到的会员信息---------"+vipInfo);
        //返回会员对象
        return vipInfo;

    }


    /**
     *  省市信息的查询,用于添加收货地址
     * @return
     */
    @RequestMapping("/getAllProvince.do")
    @ResponseBody
    public List<Province> getAllProvince(){
        //查询所有的省信息
        List<Province> allProvince = provinceService.getAllProvince();
        return allProvince;
    }


    /**
     *  根据省id查询市信息
     * @return
     */

    @RequestMapping("/getCity.do")
    @ResponseBody
    public List<City> getCityByProvinceId(Integer provinceid){
        System.out.println("获取到的省id-----"+provinceid);
        //根据省id查询市信息
        List<City> allCity = cityService.findAllCityByProvinceId(provinceid);
        System.out.println("获取市信息--------"+allCity);
        return allCity;
    }


    /**
     *  根据会员id查询会员信息
     * @return
     */
    @RequestMapping("/getvipInfoAndAddrInfoById.do")
    @ResponseBody
    public VipInfo getVipInfoById(Integer id){
        System.out.println("获取到的id信息"+id);
        VipInfo vipInfo = vipInfoService.selectByPrimaryKey(id);
        System.out.println("获取到的会员信息----"+vipInfo);
        //根据会员id查询地址信息
        AddrInfo addrInfo = addrInfoService.selectAddrInfoByid(vipInfo.getAddrId());
        System.out.println("查询到的地址信息------"+addrInfo);
        //查询省信息,根据省id
        String province=provinceService.selectProvinceByP(addrInfo.getProvinceId());
        //查询市信息
        String city=cityService.selectCityByC(addrInfo.getCityId());

        vipInfo.setAddrInfo(addrInfo.getAddrInfo());
        vipInfo.setProvinceId(addrInfo.getProvinceId());
        vipInfo.setCityId(addrInfo.getCityId());
        vipInfo.setProvince(province);
        vipInfo.setCity(city);
        System.out.println("获取会员信息------"+vipInfo);
        return vipInfo;
    }

    /**
     *  根据会员id查询代金券信息
     * @return
     */
    @RequestMapping("/selectCpnInfoByVipId.do")
    @ResponseBody
    public List<CpnInfo> selectCpnInfoByVipId(Integer id){
        //根据id查询会员编号
        VipInfo vipInfo = vipInfoService.selectByPrimaryKey(id);

        //根据会员编号查询代金券编号cpn_no
        List<CpnUseRecord> cpnUseRecordList =cpnUseRecordService.selectCpnUseRecordbyVipNo(vipInfo.getVipNo());
        //创建一个代金券的集合,粗出代金券
        List<CpnInfo> cpnInfos=new ArrayList<>();

        for (CpnUseRecord li:cpnUseRecordList
             ) {
            //获取代金券编号,查询代金券
            CpnInfo cpnInfo = cpnInfoService.selectCpnInfoByCpnNo(li.getCpnNo());
            if(cpnInfo!=null){
                cpnInfos.add(cpnInfo);
            }
        }
        System.out.println("返回的代金券信息-----"+cpnInfos);
        return cpnInfos;

    }

    /**
     * 根据会员账号查询用户的所有的收货地址
     * @return
     */

    @RequestMapping("/getAllAddrinfoByVipInfoId.do")
    @ResponseBody
    public List<AddrInfo> getAllAddrinfoByVipInfoId(String vipNo){
        System.out.println("获取会员信息--------"+vipNo);
        //根据会员账号查询所有的收货地址
        List<AddrInfo> addrInfoList = addrInfoService.getAllAddrinfoByVipInfoId(vipNo);
        for (AddrInfo addr:addrInfoList
             ) {
            String province = provinceService.selectProvinceByP(addr.getProvinceId());
            String city=cityService.selectCityByC(addr.getCityId());
            addr.setProvince(province);
            addr.setCity(city);
        }
        System.out.println("获取到的详细地址-------"+addrInfoList);
        return addrInfoList;

    }

    /**
     *  根据会员编号修改 修改账户余额(减),积分(减),代金券(已使用,删除此代金券)
     *
     * @params String vipNo(会员账号),Double accSum(账户余额),Integer accPoint(会员积分),String cpnNo(代金券编号)
     *  通过会员编号和代金券编号更新会员信息和代金券信息
     * @author shw
     * @date 17/11/25 10:09
     */

    @RequestMapping("/updateVipInfoAndCpnInfoByvipNoAndCpnNo.do")
    @ResponseBody
    public Map<String, Object> updateVipInfoAndCpnInfoByVipNoAndCpnNo(String vipNo, Double accSum, Integer accPoint, String cpnNo){
        Map<String,Object> map=new HashMap<>();
        System.out.println("-------------"+vipNo+"--"+accSum+"--"+accPoint+"--"+cpnNo);
        //根据会员编号修改会员积分和账户余额信息
        VipInfo vipInfo=new VipInfo();
        vipInfo.setVipNo(vipNo);
        vipInfo.setAccSum(accSum);
        vipInfo.setAccPoint(accPoint);
        //更新会员信息
        int vresult = vipInfoService.updateVipInfoPointSumByVipNo(vipInfo);
        System.out.println("会员信息跟新结果------"+vresult);

        if (cpnNo!=""){
            System.out.println("使用代金券------------");
            //根据会员编号和代金券编号更新会员和代金券的中间表信息
            CpnUseRecord cpnUseRecord=new CpnUseRecord();
            //查询订单最大编号,加一作为本次订单编号
            String maxOrderNo = cpnUseRecordService.selectMaxOrderNo();
            int parseInt = Integer.parseInt(maxOrderNo)+1;
            cpnUseRecord.setOrderNo(String.valueOf(parseInt));
            cpnUseRecord.setVipNo(vipNo);
            cpnUseRecord.setCpnNo(cpnNo);
            cpnUseRecord.setUsedDate(new Date());
            //执行更新会员和代金券中间表信息
            int curresult = cpnUseRecordService.updateCpnUseRecordByCpnNoVipNo(cpnUseRecord);
            System.out.println("中间表更新结果------"+curresult);
            //修改代金券信息,将代金券置为不可用
            CpnInfo cpnInfo=new CpnInfo();
            cpnInfo.setCpnNo(cpnNo);
            cpnInfo.setIsUsed("n");
            int ciresult = cpnInfoService.updateCpnInfoByCpnNo(cpnInfo);
            System.out.println("代金券表跟新结果-----"+ciresult);

            if(vresult>0&&curresult>0&&ciresult>0){
                map.put("result","success");
                System.out.println("---------更新成功");
                return map;
            }else {
                map.put("result","false");
                System.out.println("---------更新失败");
                return map;
            }
        }
        if(vresult>0){
            map.put("result","success");
            System.out.println("---------会员表更新成功");
            return map;
        }
        map.put("result","false");
        System.out.println("---------会员表更新失败");
        return map;
    }

}
